package com.newvpn.fastbest.zenith.utils;

import android.content.Context;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class AppsLimitUtils {

    private static String FILE = "vpn_apps";

    public static void saveConfig(String packname){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try (FileWriter writer = new FileWriter(FILE, true)) {
                    writer.append(packname).append(";");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void deleteConfig(Context context,String packname){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String info = getConfig(context);
                info = info.replace(packname+";","");
                saveConfig(info);

            }
        }).start();
    }

    public static String getConfig(Context context){
        StringBuilder text = new StringBuilder();
        try (InputStream inputStream = context.openFileInput(FILE)) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                text.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return text.toString();
    }
}
